{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ef1ee341-cd4d-4e97-9f96-4d2220dc4fd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bd38a43d8a0d49ad8a288d1f58a79001",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=350, width=1000)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import random\n",
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "import numpy as np\n",
    "\n",
    "rabbit_success=0  # 兔子获胜次数\n",
    "rabbit_fail=0     # 乌龟获胜次数\n",
    "t_rabbit=0     # 兔子相关时间变量（未使用）\n",
    "x=2000\n",
    "\n",
    "myCanvas=turtle.Canvas(width=1000,height=350)\n",
    "display(myCanvas)  # 显示画布\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")  # 设置背景色为浅绿色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "969f3119-0a24-413f-bd7d-33ead076c3eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "jess=turtle.Turtle(myTS)\n",
    "jess.shape(\"square\")  # 兔子用正方形表示\n",
    "jess.color(\"white\")   # 兔子颜色为白色\n",
    "jess.penup()          # 抬起画笔（移动时不画线）\n",
    "jess.goto(-200,50)    # 初始位置\n",
    "jess.pendown()        # 放下画笔\n",
    "\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")   # 乌龟用乌龟形状表示\n",
    "bob.color(\"black\")    # 乌龟颜色为黑色\n",
    "bob.penup()           # 抬起画笔\n",
    "bob.goto(-200,-50)    # 初始位置（在兔子下方）\n",
    "bob.pendown()         # 放下画笔\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3c9c62ff-5b45-4d4e-8242-e8f64af837b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "乌龟赢了\n",
      "乌龟赢了\n",
      "兔子赢了\n",
      "兔子赢了\n",
      "乌龟赢了\n",
      "乌龟赢了\n",
      "兔子赢了\n",
      "乌龟赢了\n",
      "兔子赢了\n",
      "兔子赢了\n",
      "0.5\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    v_t=random.uniform(5,20)    # 乌龟速度范围\n",
    "    v_r=random.uniform(500,750)    # 兔子速度范围\n",
    "    time_out=random.randint(100,200)# 兔子休息时间\n",
    "    jess.penup()\n",
    "    jess.goto(-200,50)\n",
    "    jess.pendown()\n",
    "    bob.penup()\n",
    "    bob.goto(-200,-50)\n",
    "    bob.pendown()\n",
    "    jess_distance=v_r  # 兔子距离\n",
    "    bob_distance=0     # 乌龟距离\n",
    "    t=0                # 时间计数\n",
    "    rest_time=0        # 兔子休息计时\n",
    "    while bob_distance<x and jess_distance<x:\n",
    "        t+=1\n",
    "        # 兔子行为逻辑\n",
    "        if rest_time>0:  # 如果在休息中\n",
    "            rest_time-=1\n",
    "        else:  # 否则前进\n",
    "            jess.forward(v_r*0.3)  # 移动（乘以0.3是为了适应画布比例）\n",
    "            jess_distance+=v_r\n",
    "            # 每跑700距离，兔子会休息一次\n",
    "            if jess_distance//700>(jess_distance-v_r)//700:\n",
    "                rest_time=time_out\n",
    "        \n",
    "        # 乌龟行为逻辑（一直前进）\n",
    "        bob_distance+=v_t\n",
    "        bob.forward(v_t*0.3)\n",
    "    \n",
    "    # 判断胜负并计数\n",
    "    if jess_distance>=x:\n",
    "        rabbit_success+=1\n",
    "        print(\"兔子赢了\")\n",
    "    elif bob_distance>=x:\n",
    "        rabbit_fail+=1\n",
    "        print(\"乌龟赢了\")\n",
    "\n",
    "# 输出乌龟获胜的概率\n",
    "print(rabbit_fail/10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "685afedc-d0e3-4ee7-a0b4-c68c287dc872",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
